<?php
/**
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2007-02-10
 * @package rail
 */
class rail_user extends rail_object {

	/**
	 * Attributes which are not writeable from outside. The array contains only the name
	 * of protected attributes not the attribute itself.
	 *
	 * @var array
	 */
	protected $protected_attributes = array( OBJ_TIME_CREATE, OBJ_TIME_LASTWRITE, OBJ_NAME, RAIL_PASSWD );


	/**
	 * Delete object and recrusiv the inventory. Returned false, if one ore more object can't deleted.
	 * The root and guest user can't deleted.
	 * The user loged in at actual rail_connector can't deleted (a user can't delete himself).
	 *
	 * @exception rail_writepermission_exception
	 * @return bool
	 */
	public function delete () {
		if( $this->rail_connector->get_user()->get_id() == $this->get_id() )
			throw new rail_writepermission_exception( "Can't delete yourself!" );
		if( $this->get_name() == "root" || $this->get_name() == "guest" )
			throw new rail_writepermission_exception( "Can't delete " . $this->get_name() . "!" );

		echo parent::delete();
	}


	/**
	 * Get all groups that has as RAIL_MEMBERSHIP to this user and the group for this user object.
	 * Returned an array of rail_group.
	 *
	 * @exception rail_readpermission_exception
	 * @return array
	 */
	public function get_memberships () {
		// check read permission
		if( !$this->is_readable( $this->rail_connector->get_user() ) )
		 	throw new rail_readpermission_exception( $this, $this->rail_connector->get_user() );

		return array_merge( array($this->get_owner_group()), $this->get_relations( "RAIL_MEMBERSHIP" ) );
	}


	/**
	 * Get users passwd.
	 *
	 * @exception rail_readpermission_exception
	 * @return string
	 */
	public function get_passwd () {
		// check read permission
		 if( !$this->is_readable( $this->rail_connector->get_user() ) )
		 	throw new rail_readpermission_exception( $this, $this->rail_connector->get_user() );

		return $this->get_attribute( RAIL_PASSWD );
	}


	/**
	 * Set user name.
	 * In every inventory can only one user with the same name. The root and guest user can't rename.
	 *
	 * @exception rail_writepermission_exception
	 * @param string $pName.
	 */
	public function rename ( $pName ) {
		// check write permission
		if( !$this->is_writeable( $this->rail_connector->get_user() ) )
		 	throw new rail_writepermission_exception( $this, $this->rail_connector->get_user() );

		$name = trim( $pName );

		if( $this->cache[ "attributes" ][OBJ_NAME] == $name )
			return;

		// can't rename root or guest account
		if( $this->get_name() == "root" || $this->get_name() == "guest" )
			throw new rail_writepermission_exception( "Can't rename " . $this->get_name() . "!" );

		// check wheather the new name already exist
		if( count( $this->rail_connector->get_root()->get_user_container()->inventory_search("/$name/", array(OBJ_NAME)) ) > 0 )
			throw new rail_writepermission_exception( "User $name already exist!" );

		// write to databases
		$this->rail_connector->send_request( "UPDATE objects_attributes SET `value`='$name' WHERE `object`='" . $this->get_id() . "' AND `attribute`='OBJ_NAME'" );
		$this->cache[ "attributes" ][OBJ_NAME] = $name;
	}


	/**
	 * Set user passwd.
	 * The guest password can't change.
	 *
	 * @exception rail_writepermission_exception
	 * @param string $pPasswd
	 */
	public function set_passwd ( $pPasswd ) {
		// check write permission
		 if( !$this->is_writeable( $this->rail_connector->get_user() ) )
		 	throw new rail_writepermission_exception( $this, $this->rail_connector->get_user() );

		if( $this->get_name() == "guest" )
			throw new rail_writepermission_exception( "Can't change guest's password!", 0 );

		$passwd = md5( $pPasswd );

		// write to databases
		$this->rail_connector->send_request( "UPDATE objects_attributes SET `value`='$passwd' WHERE `object`='" . $this->get_id() . "' AND `attribute`='RAIL_PASSWD'" );

		// update attribute cache
		$this->cache[ "attributes" ][ RAIL_PASSWD ] = $passwd;
	}

} // End of rail_user
?>